#include "Camera.h"

using namespace std;
using namespace me;

Camera::Camera(me::Vector2f worldPos):
	m_worldPos(worldPos), m_lockedEntity(NULL)
{
	//ctor
}

Camera::~Camera()
{
	//dtor
}

Vector2f Camera::getWorldPos()
{
	return m_worldPos;
}

me::Entity* Camera::getLockedEntity()
{
    return m_lockedEntity;
}

me::Vector2f Camera::getLockedOffset()
{
    return m_lockedOffset;
}


void Camera::setWorldPos(me::Vector2f pos)
{
	m_worldPos = pos;
}

void Camera::moveWorldPos(me::Vector2f movement)
{
	m_worldPos += movement;
}

void Camera::centerOn(sf::RenderWindow* window, me::World* world, me::Vector2f pos)
{
    if ( (world != NULL) && (window != NULL) )
    {
        me::Vector2f screenPos = pos;
        screenPos.x *= world->getTileSize().x;
        screenPos.y *= world->getTileSize().y;
        sf::Vector2u window_size = window->getSize();
        m_worldPos = pos;
        m_worldPos.x -= window_size.x/world->getTileSize().x/2;
        m_worldPos.y -= window_size.y/world->getTileSize().y/2;
    }
}

void Camera::lockOn(me::Entity* entity, me::Vector2f offset)
{
    m_lockedEntity = entity;
    m_lockedOffset = offset;
}

void Camera::unlock()
{
    m_lockedEntity = NULL;
}

void Camera::setLockedOffset(me::Vector2f offset)
{
    m_lockedOffset = offset;
}


void Camera::draw(sf::RenderWindow* window, me::World* world)
{
    if ( (window != NULL) && (world != NULL) )
    {
        vector<Entity*> entities = world->getEntities();
        for (unsigned int i=0 ; i < entities.size() ; i++)
        {
            me::Vector2f screenPos = entities[i]->getWorldPosition()-m_worldPos;
            screenPos.x *= world->getTileSize().x;
            screenPos.y *= world->getTileSize().y;
            entities[i]->setPosition(sf::Vector2f(screenPos.x, screenPos.y));
            window->draw(*entities[i]);
            sf::Vector2f pos = entities[i]->getPosition();
            cout << pos.x << ", " << pos.y << endl;
        }

        if (m_lockedEntity != NULL)
            this->centerOn(window, world, m_lockedEntity->getWorldPosition()+m_lockedOffset);
    }

}
